
	@	/0000
P	JP	INI
;variaveis fixas
CARREGA	K	/8000
SALVA	K	/9000
VOLTA	K	/0000
DESVIA	K	/0000
END	K	/0000
TAMRA	K	/0000
EATUAL	K	/0000
EREND	K	/0000
NEG	K	/2D00
NEG2	K	/002D
K8	K	/8000
MUL	K	/100
DMIL	K	/2710
MIL	K	/3E8
CEM	K	/64
DEZ	K	/A
UM	K	/1
MNUM	K	/FFFF
TRIN	K	/30
LIN	K	/0D0A
CRT	K	/000D
MOD1	K	/000
MOD2	K	/000
MOD3	K	/000
PRINT1	K	/000
PRINT2	K	/000
MODR	K	/000
HBYTE1	K	/000
HBYTE2	K	/000
HBYTER	K	/000
READ1	K	/000
READ2	K	/000
READN	K	/000
READR	K	/000


;retorna um endereco
REND	JP	/000
	LD	EATUAL
	MM	EREND
LOOPRE	LD	VOLTA
	JZ	CONTRE
	LV	/4
	+	EREND
	+	CARREGA
	MM	SEREND	;salva EREND
SEREND	JP	/000
	MM	EREND
	LD	VOLTA
	-	UM
	MM	VOLTA
	JP	LOOPRE
CONTRE	LD	EREND
	+	DESVIA
	MM	END
	RS	REND


;empilha RA
EMP	JP	/000
	LV	/4
	+	TAMRA
	+	EATUAL
	+	SALVA
	MM	PACS	;Ponto de Acesso Salva
	LD	EATUAL
PACS	JP	/000
	LD	EATUAL
	+	TAMRA
	MM	EATUAL
	RS	EMP


;desempilha Ra
DEMP	JP	/000
	LV	/4
	+	EATUAL
	+	CARREGA
	MM	PACC	;Ponto de Acesso Carrega
PACC	JP	/000
	MM	EATUAL
	RS	DEMP


;calculo para o printf
MOD	JP	/000
	LD	DEZ
	*	MOD2
	MM	MOD3
	LD	MOD1
	/	MOD3
	*	MOD3
	MM	MOD3
	LD	MOD1
	-	MOD3
	/	MOD2
	MM	MODR
	RS	MOD


;imprime numeros
PRINT	JP	/000
	LD	PRINT1
	-	K8
	JN	CALC
	LD	NEG
	MM	PRINT2
	LD	PRINT1
	*	MNUM
	MM	PRINT1
CALC	LD	PRINT1
	MM	MOD1
	/	DMIL
	+	TRIN
	+	PRINT2
	MM	PRINT2
	PD	/100
	LD	MIL
	MM	MOD2
	SC	MOD
	LD	MODR
	+	TRIN
	*	MUL
	MM	PRINT2
	LD	CEM
	MM	MOD2
	SC	MOD
	LD	MODR
	+	TRIN
	+	PRINT2
	PD	/100
	LD	DEZ
	MM	MOD2
	SC	MOD
	LD	MODR
	+	TRIN
	*	MUL
	MM	PRINT2
	LD	UM
	MM	MOD2
	SC	MOD
	LD	MODR
	+	TRIN
	+	PRINT2
	PD	/100
	RS	PRINT


;pega o valor lido no byte a esquerda
HBYTE	JP	/000
	LD	HBYTE1
	/	MUL
	*	MUL
	MM	HBYTE2
	LD	HBYTE1
	-	HBYTE2
	MM	HBYTER
	RS	HBYTE


;le numero
READ	JP	/000
	LV	/0
	MM	READR	;seta saida como 0
	LV	/1
	MM	READN	;seta negativo como 1
RDMS	GD	/000
	MM	READ1
	/	MUL
	MM	READ2
	-	CRT	;li CR?
	JZ	RDFIM
	LD	READ2
	-	NEG2	;li um -?
	JZ	RDNEG
	LD	READ2
	-	TRIN
	MM	READ2
	LD	READR
	*	DEZ
	+	READ2
	MM	READR	;somo na resposta
	JP	RDC1
RDNEG	LD	MNUM
	MM	READN	;se eu li um -, seto negativo como -1
RDC1	LD	READ1
	MM	HBYTE1
	SC	HBYTE
	LD	HBYTER
	-	CRT	;li CR?
	JZ	RDFIM
	LD	HBYTER
	-	TRIN
	MM	HBYTER
	LD	READR
	*	DEZ
	+	HBYTER
	MM	READR	;somo na resposta
	JP	RDMS	;le mais
RDFIM	LD	READR
	*	READN
	MM	READR
	RS	READ


;Funcao fatorialR
F_fatorialR	JP	/000
	LV	/000
	MM	VOLTA
	LV	/2
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	FSER1
	LD	F_fatorialR
FSER1	JP	/000


	LV	/0
	MM	VOLTA
	LV	/8
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR1
CARR1	JP	/000
	MM 	TEMP1


	LV	/1
	MM 	TEMP2


	LD	TEMP1
	- 	TEMP2
	JN	SAIUM1
	JZ	SAIUM1
	LV	/0
	JP	FIMCMP1
SAIUM1	LV	/1
FIMCMP1	MM	TEMP1


	LV	/c
	MM	TAMRA
	SC	EMP
	LD	TEMP1	;inicio if
	JZ	ELSE1


	LV	/1
	MM 	TEMP1


	LV	/1
	MM	VOLTA
	LV	/a
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	ATR1
	LD	TEMP1
ATR1	JP	/000


	JP	FIMSE1
ELSE1	LV	/000	;inicio else


	LV	/a
	MM	TAMRA
	SC	EMP


	LV	/1
	MM 	TEMP1
	LV	/2
	MM	VOLTA
	LV	/8
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR2
CARR2	JP	/000
	- 	TEMP1
	MM 	TEMP1


	LV	/0	;guardo parametro 1
	MM	VOLTA
	LV	/8
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	ATR2
	LD	TEMP1
ATR2	JP	/000


	LV	/0
	MM	VOLTA
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	FSEVR1
	LV	/1
	MM	VOLTA
	LV	/8
	MM	DESVIA
	SC	REND
	LD	END
FSEVR1	JP	/000
	SC	F_fatorialR
	SC	DEMP
	LV	/0
	MM	VOLTA
	LV	/8
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR3
CARR3	JP	/000
	MM	TEMP1


	LV	/1
	MM	VOLTA
	LV	/a
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	ATR3
	LD	TEMP1
ATR3	JP	/000


	LV	/1
	MM	VOLTA
	LV	/a
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR4
CARR4	JP	/000
	MM	TEMP1
	LV	/1
	MM	VOLTA
	LV	/8
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR5
CARR5	JP	/000
	*	TEMP1
	MM 	TEMP1


	LV	/1
	MM	VOLTA
	LV	/a
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	ATR4
	LD	TEMP1
ATR4	JP	/000


FIMSE1	SC	DEMP	;fim if-else


	LV	/0
	MM	VOLTA
	LV	/a
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR6
CARR6	JP	/000
	MM 	TEMP1


	LV	/000
	MM	VOLTA
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR7
CARR7	JP	/00
	+	SALVA
	MM	FSVR1
	LD	TEMP1
FSVR1	JP	/00
	LV	/000
	MM	VOLTA
	LV	/2
	MM	DESVIA
	SC	REND
	RS	END




;Funcao fatorialI
F_fatorialI	JP	/000
	LV	/000
	MM	VOLTA
	LV	/2
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	FSER2
	LD	F_fatorialI
FSER2	JP	/000


	LV	/1
	MM 	TEMP1


	LV	/0
	MM	VOLTA
	LV	/a
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	ATR5
	LD	TEMP1
ATR5	JP	/000


	LV	/2
	MM 	TEMP1


	LV	/0
	MM	VOLTA
	LV	/c
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	ATR6
	LD	TEMP1
ATR6	JP	/000


	LV	/0
	MM	VOLTA
	LV	/8
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR8
CARR8	JP	/000
	MM 	TEMP1


	LV	/1
	MM 	TEMP2


	LD	TEMP1
	- 	TEMP2
	JN	SAIUM2
	JZ	SAIUM2
	LV	/0
	JP	FIMCMP2
SAIUM2	LV	/1
FIMCMP2	MM	TEMP1


	LV	/e
	MM	TAMRA
	SC	EMP
	LD	TEMP1	;inicio if
	JZ	ELSE2


	LV	/1
	MM 	TEMP1


	LV	/1
	MM	VOLTA
	LV	/a
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	ATR7
	LD	TEMP1
ATR7	JP	/000


	JP	FIMSE2
ELSE2	LV	/000	;inicio else


;prepara while
INIEQ1	LV	/000
	LV	/1
	MM	VOLTA
	LV	/c
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR9
CARR9	JP	/000
	MM 	TEMP1


	LV	/1
	MM	VOLTA
	LV	/8
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR10
CARR10	JP	/000
	MM 	TEMP2


	LD	TEMP1
	- 	TEMP2
	JN	SAIUM3
	JZ	SAIUM3
	LV	/0
	JP	FIMCMP3
SAIUM3	LV	/1
FIMCMP3	MM	TEMP1


;inicia while
	LD	TEMP1	;ini enquanto
	JZ	FIMEQ1
	LV	/8
	MM	TAMRA
	SC	EMP


	LV	/2
	MM	VOLTA
	LV	/c
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR11
CARR11	JP	/000
	MM	TEMP1
	LV	/2
	MM	VOLTA
	LV	/a
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR12
CARR12	JP	/000
	*	TEMP1
	MM 	TEMP1


	LV	/2
	MM	VOLTA
	LV	/a
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	ATR8
	LD	TEMP1
ATR8	JP	/000


	LV	/1
	MM 	TEMP1
	LV	/2
	MM	VOLTA
	LV	/c
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR13
CARR13	JP	/000
	+ 	TEMP1
	MM 	TEMP1


	LV	/2
	MM	VOLTA
	LV	/c
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	ATR9
	LD	TEMP1
ATR9	JP	/000


;fim while
	SC	DEMP	
	JP	INIEQ1
FIMEQ1	LV	/000	;fim enquanto


FIMSE2	SC	DEMP	;fim if-else


	LV	/0
	MM	VOLTA
	LV	/a
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR14
CARR14	JP	/000
	MM 	TEMP1


	LV	/000
	MM	VOLTA
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR15
CARR15	JP	/00
	+	SALVA
	MM	FSVR2
	LD	TEMP1
FSVR2	JP	/00
	LV	/000
	MM	VOLTA
	LV	/2
	MM	DESVIA
	SC	REND
	RS	END




;INICIO PROGRAMA
INI	LV	RAINI
	MM	EATUAL
	LV	/0
	MM	VOLTA
	LV	/8
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	ATR10
	SC	READ
	LD	READR
ATR10	JP	/000


	LV	/e
	MM	TAMRA
	SC	EMP


	LV	/1	;carrego parametro 1
	MM	VOLTA
	LV	/8
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR16
CARR16	JP	/000
	MM 	TEMP1


	LV	/0	;e o guardo
	MM	VOLTA
	LV	/8
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	ATR11
	LD	TEMP1
ATR11	JP	/000


	LV	/0
	MM	VOLTA
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	FSEVR2
	LV	/1
	MM	VOLTA
	LV	/c
	MM	DESVIA
	SC	REND
	LD	END
FSEVR2	JP	/000
	SC	F_fatorialR
	SC	DEMP
	LV	/0
	MM	VOLTA
	LV	/c
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR17
CARR17	JP	/000
	MM	TEMP1


	LV	/0
	MM	VOLTA
	LV	/a
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	ATR12
	LD	TEMP1
ATR12	JP	/000


	LV	/72
	MM	TEMP1
	LV	/100
	*	TEMP1
	MM	TEMP1
	LV	/0A
	+	TEMP1
	PD	/100


	LV	/0
	MM	VOLTA
	LV	/a
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR18
CARR18	JP	/000
	MM 	TEMP1


	LD	TEMP1
	MM	PRINT1
	LV	/0
	MM	PRINT2
	SC	PRINT
	LD	LIN
	PD	/100


	LV	/e
	MM	TAMRA
	SC	EMP


	LV	/1	;carrego parametro 1
	MM	VOLTA
	LV	/8
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR19
CARR19	JP	/000
	MM 	TEMP1


	LV	/0	;e o guardo
	MM	VOLTA
	LV	/8
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	ATR13
	LD	TEMP1
ATR13	JP	/000


	LV	/0
	MM	VOLTA
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	FSEVR3
	LV	/1
	MM	VOLTA
	LV	/c
	MM	DESVIA
	SC	REND
	LD	END
FSEVR3	JP	/000
	SC	F_fatorialI
	SC	DEMP
	LV	/0
	MM	VOLTA
	LV	/c
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR20
CARR20	JP	/000
	MM	TEMP1


	LV	/0
	MM	VOLTA
	LV	/a
	MM	DESVIA
	SC	REND
	LD	END
	+	SALVA
	MM	ATR14
	LD	TEMP1
ATR14	JP	/000


	LV	/69
	MM	TEMP1
	LV	/100
	*	TEMP1
	MM	TEMP1
	LV	/0A
	+	TEMP1
	PD	/100


	LV	/0
	MM	VOLTA
	LV	/a
	MM	DESVIA
	SC	REND
	LD	END
	+	CARREGA
	MM	CARR21
CARR21	JP	/000
	MM 	TEMP1


	LD	TEMP1
	MM	PRINT1
	LV	/0
	MM	PRINT2
	SC	PRINT
	LD	LIN
	PD	/100


	HM	/00
TEMP1	K	/000
TEMP2	K	/000
RAINI	K	/000
	#	P

